package com.xai.pg.data;

import com.xai.pg.util.Convert;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xxx
 * @Date 2018-12-24 14:12:58
 * @Desc DBTable
 *
 */
public class DBTable {

    private String tableName;
    private String cName;
    private String dName;
    private String pathName;
    private String desc;
    private DBField pk;
    private List<DBField> fields = new ArrayList<>();

    private String create;
    private String update;

    public void generatorSQL() {
        this.generatorCreateSQL();
        this.generatorUpdateSQL();
    }

    private void generatorCreateSQL() {
        StringBuilder sb = new StringBuilder();
        sb.append("insert into ").append(tableName).append(" (");

        StringBuilder columns = new StringBuilder();
        fields.stream().forEach(field -> {
            columns.append(field.getName()).append(", ");
        });
        columns.deleteCharAt(columns.lastIndexOf(","));

        sb.append(columns).append(") values (");

        StringBuilder values = new StringBuilder();
        fields.stream().forEach(field -> {
            values.append("#{").append(field.getcName()).append("}, ");
        });
        values.deleteCharAt(values.lastIndexOf(","));
        sb.append(values).append(");");
        this.create = sb.toString();
    }

    private void generatorUpdateSQL() {
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(tableName).append(" set ");
        fields.stream().forEach(field -> {
            String name = field.getcName().trim().toLowerCase();
            if (!"id".equals(name)) {
                sb.append(field.getName()).append(" = #{").append(field.getcName()).append("}, ");
            }
        });
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append(" where id = #{id}");
        this.update = sb.toString();
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
        if (tableName != null) {
            if (tableName.startsWith("tab")) {
                tableName = tableName.substring(3, tableName.length());
            }
            if (tableName.endsWith("tab")) {
                tableName = tableName.substring(0, tableName.length() - 3);
            }
            this.setPathName(tableName.replace("_", "/"));
            String str = Convert.up(tableName, true);
            this.setcName(str);
        }
    }

    public DBField getPk() {
        return pk;
    }

    public void setPk(DBField pk) {
        this.pk = pk;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public List<DBField> getFields() {
        return fields;
    }

    public void addFields(DBField field) {
        this.fields.add(field);
    }

    public String getcName() {
        return cName;
    }

    public void setcName(String cName) {
        this.cName = cName;
        if (cName != null) {
            this.setdName(cName + "Data");
        }
    }

    public String getPathName() {
        return pathName;
    }

    public void setPathName(String pathName) {
        this.pathName = pathName;
    }

    public String getdName() {
        return dName;
    }

    public void setdName(String dName) {
        this.dName = dName;
    }

    public String getCreate() {
        return create;
    }

    public void setCreate(String create) {
        this.create = create;
    }

    public String getUpdate() {
        return update;
    }

    public void setUpdate(String update) {
        this.update = update;
    }
}
